package com.hktf.uilib.widget;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hktf.corelib.utils.CommUtils;
import com.hktf.corelib.utils.UIUtils;
import com.hktf.uilib.R;


public class PickPopupWindow<T> extends PopupWindow {
	private View conentView;
	private RecyclerView recyclerView;

	public PickPopupWindow(final Activity context, BaseQuickAdapter<T, BaseViewHolder> adapter, int width,int height, Drawable bg, int lineColor,boolean showLine3,final PickListener<T> listener) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		conentView = inflater.inflate(R.layout.dl_pickpop, null);

		// 设置SelectPicPopupWindow的View
		this.setContentView(conentView);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(width);
		// 设置SelectPicPopupWindow弹出窗体的高
		if (height==0) {
//			this.setHeight(LayoutParams.WRAP_CONTENT);
			this.setHeight(UIUtils.dip2px(100));
		}else {
			this.setHeight(UIUtils.dip2px(200));
		}
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		// 刷新状态
		this.update();
		// 实例化一个ColorDrawable颜色为半透明
//		ColorDrawable dw = new ColorDrawable(0xb0000000);
		// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
		if (bg == null) {
			this.setBackgroundDrawable(CommUtils.getDrawable(R.drawable.black_bg));
		}else {
			this.setBackgroundDrawable(bg);
		}
		// mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		//this.setAnimationStyle(R.style.AnimationPreview);
		View line3=conentView.findViewById(R.id.line3);
		if (lineColor!=0) {
			View line1=conentView.findViewById(R.id.line1);
			View line2=conentView.findViewById(R.id.line2);
			line1.setBackgroundColor(lineColor);
			line2.setBackgroundColor(lineColor);
			line3.setBackgroundColor(lineColor);
		}
		line3.setVisibility(showLine3?View.VISIBLE:View.GONE);


		recyclerView=conentView.findViewById(R.id.recyclerView);
		recyclerView.setLayoutManager(new LinearLayoutManager(context));
		recyclerView.setAdapter(adapter);
		adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
				int height = recyclerView.getHeight();
//				startAnimator(height, 0,recyclerView);
				if (listener != null) {
					listener.onPick((T) adapter.getData().get(position),position);
				}
				if (isShowing()) {
					dismiss();
				}
			}
		});
		conentView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (isShowing()) {
					dismiss();
				}
				if(listener!=null){
					listener.onCancel();
				}
			}
		});
	}

//	@Override
//	public void showAsDropDown(View anchor) {
//		super.showAsDropDown(anchor);
//		recyclerView.setVisibility(View.VISIBLE);
//	}

	private boolean isAnimationFinish;
	private void startAnimator(int start, final int end,final RecyclerView recyclerView) {
		ValueAnimator animator = ValueAnimator.ofInt(start, end);
		animator.setDuration(200);
		animator.setInterpolator(new LinearInterpolator());//线性效果变化
		animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animator) {
				Integer integer = (Integer) animator.getAnimatedValue();
				LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) recyclerView.getLayoutParams();
				layoutParams.height = integer;
				recyclerView.requestLayout();
				if (integer == end) {
					isAnimationFinish = true;
				}
			}
		});
		animator.start();
	}

	public interface PickListener<T>{
		void onPick(T data,int position);
		void onCancel();
	}

}
